package com.zbkj.admin.boot.redisson.stream;

import com.zbkj.admin.boot.redisson.common.RedisNameResolver;
import net.dreamlu.mica.core.constant.MicaConstant;
import net.dreamlu.mica.core.utils.StringUtil;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * Redisson 延迟队列配置
 *
 * @author L.cm
 */
@Configuration
@ConditionalOnBean(RedissonClient.class)
@EnableConfigurationProperties(RedissonStreamProperties.class)
public class RedissonStreamConfiguration {

    @Bean
    public RStreamSender streamSender(RedissonClient client,
                                      RedisNameResolver resolver) {
        return new RedissonStreamSender(client, resolver);
    }

    @Bean
    public RStreamListenerDetector streamListenerDetector(RedissonClient client,
                                                          RedisNameResolver resolver,
                                                          Environment environment,
                                                          RedissonStreamProperties properties) {
        String groupId = properties.getConsumer().getGroupId();
        if (StringUtil.isBlank(groupId)) {
            groupId = environment.getRequiredProperty(MicaConstant.SPRING_APP_NAME_KEY);
        }
        return new RStreamListenerDetector(client, resolver, groupId);
    }
}
